//
//  LHMultipleSelectAlertView.m
//  Loohoo
//
//  Created by 陈镇 on 2025/4/16.
//

#import "LHMultipleSelectAlertView.h"
#import "LHMultipleSelectAlertCell.h"


@interface LHMultipleSelectAlertView ()<UITableViewDelegate,UITableViewDataSource>

@property(nonatomic,strong) NSMutableArray<QMUIButton *> *buttons;
@property(nonatomic,strong) NSMutableArray<UITableView *> *tableViews;
@property(nonatomic,strong) NSMutableDictionary *selectModels;

@property(nonatomic,strong) QMUIButton *confrimBtn;


@property(nonatomic,strong) UIView *indicator;

@property(nonatomic,assign) NSInteger index;

@end

@implementation LHMultipleSelectAlertView

-(instancetype)initWithFrame:(CGRect)frame option:(LHMultipleSelectAlertViewOption *)option{
    if (self = [super initWithFrame:frame]) {
        self.option = option;
        
        self.contentFrame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT * 0.7);
        self.backgroundColor = UIColorHex(66666633);
    }
    return self;
}

//icon_common_close_w


-(void)createUI{
    
    //有几个列表
    NSInteger count = [self.delegate tableViewCount];
    
    
    //标题
    QMUILabel *title = [QMUILabel createTextColor:self.option.titleColor font:self.option.titleFont textAlignment:NSTextAlignmentCenter];
    title.text = self.option.title;
    [self.contentView addSubview:title];
    title.frame = CGRectMake(0, 20, self.contentView.width, 24);
    
    CGFloat top = title.bottom + 24;
    
    //多列表需要个中间buttons
    if (count > 1) {
        [self buttons];
       
        
        //设置分割线
        UIView *line = [[UIView alloc] initWithFrame:CGRectMake(0, 100, self.contentView.width, 0.5)];
        line.backgroundColor = UIColorHex(eeeeee);
        [self.contentView addSubview:line];
        
        self.indicator = [[UIView alloc] initWithFrame:CGRectMake(0, line.top - self.option.indicatorHeight , self.option.indicatorWidth, self.option.indicatorHeight)];
        self.indicator.backgroundColor = self.option.indicatorColor;
        [self.contentView addSubview:self.indicator];
        
        
  
        
        top = line.bottom;
    }
    
    
    //底部按钮
    QMUIButton *confrimBtn = [QMUIButton createButtonTitle:self.option.confirmText titleColor:self.option.confirmColor font:self.option.confirmFont];
    confrimBtn.backgroundColor = self.option.confirmBgColor;
    confrimBtn.frame = CGRectMake(12, self.contentView.height - 44 - bottomToViewNO(0), SCREEN_WIDTH -24, 44);
    confrimBtn.layer.cornerRadius = confrimBtn.height/2;
    confrimBtn.layer.masksToBounds = YES;
    [self.contentView addSubview:confrimBtn];
    self.confrimBtn = confrimBtn;
    [confrimBtn addTarget:self action:@selector(clickConfrim) forControlEvents:UIControlEventTouchUpInside];
    
    //创建列表
    [self tableViews];
    [self setTalbeViewFrame:top];
    if (count > 1) {
        //刷新到第一个列表
        [self clickItem:self.buttons.firstObject];
        
        //多选择的时候，默认不显示确认按钮，最后才显示确认按钮
        self.confrimBtn.hidden = YES;
    }
 
    [self downloadData];
    
}
-(NSMutableDictionary *)selectModels{
    if (!_selectModels) {
        _selectModels = [NSMutableDictionary dictionary];
    }
    return _selectModels;
}

#pragma mark - 设置button
-(NSMutableArray<QMUIButton *> *)buttons{
 
    
    
    if (!_buttons) {
        
        _buttons = [NSMutableArray array];
        NSInteger count = [self.delegate tableViewCount];
        for (int i = 0; i< count; i++) {
            
            QMUIButton *btn = [QMUIButton createButtonTitle:[self.delegate tableViewTitleCount:i] titleColor:self.option.titlesColor font:self.option.titlesFont];
            
            [btn setTitleColor:self.option.titlesSelectColor forState:UIControlStateSelected];
            
            btn.tag = i;
            btn.userInteractionEnabled = NO;
            [btn addTarget:self action:@selector(clickItem:) forControlEvents:UIControlEventTouchUpInside];
            //添加按钮到视图
            [self.contentView addSubview:btn];
            [_buttons addObject:btn];
        }
    }
    return _buttons;
}


-(void)setButtonsFrame{
    
    CGFloat left = 20;
    for (QMUIButton *btn in self.buttons) {
        [btn sizeToFit];
        btn.frame = CGRectMake(left, 64, btn.width, 22);
        left = btn.right + 20;
        
    }
    
}
//点击顶部button
-(void)clickItem:(QMUIButton *)btn{
    
    self.index = btn.tag;
    
    //更新标题 - 并且更新
    [self changeTitlesValue];
    //更新底部指示器
    self.indicator.centerX = btn.center.x;
    
    [self changeTableViewShow];
}
-(void)changeTitlesValue{
    
    
    for (int i = 0; i < self.buttons.count; i++) {
        
        
        LHMultipleSelectAlertViewCellModel *model = [self.selectModels objectForKey:@(i).stringValue];
        QMUIButton *btn = self.buttons[i];
        if (model) {
            btn.selected = YES;
            btn.userInteractionEnabled = YES;
            [btn setTitle:model.title forState:UIControlStateNormal];
            btn.titleLabel.font = self.option.titlesSelectFont;
        }else{
            btn.selected = NO;
            btn.userInteractionEnabled = NO;
            [btn setTitle:[self.delegate tableViewTitleCount:i] forState:UIControlStateNormal];
            btn.titleLabel.font = self.option.titlesFont;
        }
        
        //选到最后一个才显示底部按钮
        if (i == self.buttons.count - 1 && model) {
            self.confrimBtn.hidden = NO;
        }else{
            self.confrimBtn.hidden = YES;
        }
        
        
    }
    [self setButtonsFrame];
    
}


#pragma mark - 设置列表

-(NSMutableArray<UITableView *> *)tableViews{
    if (!_tableViews) {
        _tableViews = [NSMutableArray array];
      
        
        for (int i = 0; i< [self.delegate tableViewCount]; i++) {
            UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
            tableView.backgroundColor = UIColorWhite;
            tableView.delegate = self;
            tableView.dataSource = self;
            tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
            tableView.separatorInset = UIEdgeInsetsMake(0, 0, 0, 0);
            tableView.sectionHeaderHeight = 0;
            tableView.sectionFooterHeight = 0;
            tableView.tag = 100 + i;
            [self.contentView addSubview:tableView];
            [_tableViews addObject:tableView];
        }
        
    
    }
    return _tableViews;
}

-(void)setTalbeViewFrame:(CGFloat)top{

    for (UITableView *tableVeiw in self.tableViews) {
        tableVeiw.frame = CGRectMake(0, top, SCREEN_WIDTH,self.confrimBtn.top - top);
    }
}

-(void)changeTableViewShow{
    
    for (int i = 0; i < self.tableViews.count; i++) {
        UITableView *tableView = self.tableViews[i];
        if (i == _index) {
            tableView.hidden = NO;
          
        }else{
            tableView.hidden = YES;
        }
        [tableView reloadData];
    }
}

#pragma mark - UITableViewDelegate  UITableViewDataSource

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return [self.delegate tableViewDataWithCount:tableView.tag - 100].count;
}


-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LHMultipleSelectAlertCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LHMultipleSelectAlertCell"];
    if (!cell) {
        cell = [[LHMultipleSelectAlertCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"LHMultipleSelectAlertCell"];
        
        
        cell.cellTextFont = self.option.cellTextFont;
        cell.cellTextColor = self.option.cellTextColor;
        
        
        cell.cellSelectTextFont = self.option.cellSelectTextFont;
        cell.cellSelectTextColor = self.option.cellSelectTextColor;
        
        cell.select_default_Image = self.option.select_default_Image;
        cell.select_select_Image = self.option.select_select_Image;
        
        cell.ableSelect = self.option.ableSelect;
        
    }
    NSArray<LHMultipleSelectAlertViewCellModel *> * list =  [self.delegate tableViewDataWithCount:tableView.tag - 100];
    
    cell.model = list[indexPath.row];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return self.option.cellHeight;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.00001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 50;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    LHMultipleSelectAlertCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    
    
    //处理当前列表的选中
    LHMultipleSelectAlertViewCellModel *model = [self.selectModels objectForKey:@(_index).stringValue];
    model.isSelect = NO;
    cell.model.isSelect = YES;
    [self.selectModels setObject:cell.model forKey:@(_index).stringValue];
    
    [tableView reloadData];
    
    
    //清空已经选中的数据
    for (int i = 0; i < self.buttons.count ; i++) {
        if (i > _index) {
            LHMultipleSelectAlertViewCellModel *model = [self.selectModels objectForKey:@(i).stringValue];
            model.isSelect = NO;
            [self.selectModels removeObjectForKey:@(i).stringValue];
        }
    }

    //跳转到下一个页面
    _index ++;
    if (_index == self.tableViews.count) {
        _index = self.tableViews.count - 1;
        
    }else{
        [self downloadData];
    }
    if (self.delegate.tableViewCount > 1) {
        //跳转到下一个页面
        [self clickItem:self.buttons[_index]];
    }
 
 
    
}

-(void)downloadData{
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(downloadDataWithCount:tableView:selectModels:)]) {
        [self.delegate downloadDataWithCount:_index tableView:self.tableViews[_index] selectModels:self.selectModels];
    }
    
}




-(void)startView{
    [self createUI];
    [self sl_showBottomSubView:self.contentView];
}




-(void)clickConfrim{
    
    
    if (self.selectModels.allKeys.count == 0) {
        SLTipToWindow(@"请选择内容");
        return;
    }
    
    
    //传值
    if (self.resultBlock) {
        self.resultBlock(self.selectModels);
    }
    
    [self disMissView];
    
}

@end
